package com.ruoyi.web.controller;

import cn.hutool.core.util.NumberUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.domain.TProjectPayment;
import com.ruoyi.web.service.ITProjectPaymentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.NumberFormat;
import java.util.List;

/**
 * 项目支付信息Controller
 *
 * @author ruoyi
 * @date 2025-03-08
 */
@Controller
@RequestMapping("/web/payment")
public class TProjectPaymentController extends BaseController {
    private String prefix = "web/payment";

    @Autowired
    private ITProjectPaymentService tProjectPaymentService;

    @RequiresPermissions("web:payment:view")
    @GetMapping()
    public String payment() {
        return prefix + "/payment";
    }

    /**
     * 查询项目支付信息列表
     */
    @RequiresPermissions("web:payment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TProjectPayment tProjectPayment) {
        startPage();
        List<TProjectPayment> list = tProjectPaymentService.selectTProjectPaymentList(tProjectPayment);
        list.parallelStream().forEach(t -> t.setDisProjectAmount(NumberUtil.decimalFormat("￥#,##0.00", Double.valueOf(t.getProjectAmount()))));
        return getDataTable(list);
    }

    /**
     * 导出项目支付信息列表
     */
    @RequiresPermissions("web:payment:export")
    @Log(title = "项目支付信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TProjectPayment tProjectPayment) {
        List<TProjectPayment> list = tProjectPaymentService.selectTProjectPaymentList(tProjectPayment);
        list.parallelStream().forEach(t -> t.setDisProjectAmount(NumberUtil.decimalFormat("￥#,##0.00", t.getProjectAmount())));
        ExcelUtil<TProjectPayment> util = new ExcelUtil<TProjectPayment>(TProjectPayment.class);
        return util.exportExcel(list, "项目支付信息数据");
    }

    /**
     * 新增项目支付信息
     */
    @RequiresPermissions("web:payment:add")
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存项目支付信息
     */
    @RequiresPermissions("web:payment:add")
    @Log(title = "项目支付信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TProjectPayment tProjectPayment) {
        tProjectPayment.setCreatBy(getLoginName());
        return toAjax(tProjectPaymentService.insertTProjectPayment(tProjectPayment));
    }

    /**
     * 修改项目支付信息
     */
    @RequiresPermissions("web:payment:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TProjectPayment tProjectPayment = tProjectPaymentService.selectTProjectPaymentById(id);
        tProjectPayment.setUpdateBy(getLoginName());
        mmap.put("tProjectPayment", tProjectPayment);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目支付信息
     */
    @RequiresPermissions("web:payment:edit")
    @Log(title = "项目支付信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TProjectPayment tProjectPayment) {
        tProjectPayment.setUpdateBy(getLoginName());
        return toAjax(tProjectPaymentService.updateTProjectPayment(tProjectPayment));
    }

    /**
     * 删除项目支付信息
     */
    @RequiresPermissions("web:payment:remove")
    @Log(title = "项目支付信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tProjectPaymentService.deleteTProjectPaymentByIds(ids));
    }
}
